programming4us
           
 
 
Windows Phone

Developing for Windows Phone and Xbox Live : Multiplayer Games (part 5) - Searching for an Available Network Session

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/18/2011 5:50:18 PM

Searching for an Available Network Session

So now you can create a NetworkSession and start the game, but you are missing the capability for other players to join your game. You need to search for the available network sessions and then join one of the sessions. After you join, you have already created the code to handle playing and quitting the game.

To wire up the find session logic, add code to the MainMenuUpdate method to check for the B button that is pressed.

// Find a session
if (ButtonPressed(Buttons.B))
FindSession();

When the player presses the B button at the main menu, start the search for a session to join.

To store the collection of AvailableNetworkSession objects so that you can pick the one you want to join, add the following member variable to your Game class:

// List of sessions that you can join
AvailableNetworkSessionCollection availableSessions;

Now you need to implement the FindSession method, which populates the AvailableNetworkSessionCollection and switches out GameState to the FindSession menu screen. Add the following method to your Game class:

// Method to start the search for a NetworkSession
private void FindSession()
{
// Dispose of any previous session
if (networkSession != null && !networkSession.IsDisposed)
networkSession.Dispose();

// Define the type of session we want to search for using the
// NetworkSessionProperties.
// We only set the MapLevel and the OtherCustomProperty
NetworkSessionProperties sessionProperties = new NetworkSessionProperties();
sessionProperties[(int)SessionProperties.MapLevel] = 0;
sessionProperties[(int)SessionProperties.OtherCustomProperty] = 42;

// Find an available NetworkSession
availableSessions = NetworkSession.Find(NetworkSessionType.SystemLink,
1, sessionProperties);

// Move the game into the FindSession state
gameState = GameState.FindSession;
}


FindSession first checks whether there is a current active NetworkSession and calls Dispose on the session if it is active.

As you did when you created the NetworkSession, create a NetworkSessionProperties object. This time, the properties are used to search for a specific session. You don’t need to specify all of the properties. For example, you don’t set a GameType value so the sessions returned can be of any GameType.

To generate the list of AvailableNetworkSession instances, call the NetworkSession.Find method. Find takes three arguments. The first is the NetworkSessionTypeCreate method. The second is the number of local players that will be joining you in the session. Specify just 1 for this example. Finally the third and final argument is the NetworkSessionProperties to use in the search. just like you used for the

The resulting AvailableNetworkSessionCollection contains all of the AvailableNetworkSession instances that match the properties and have room for you to join.

Note

Like the NetworkSession.Create method, the NetworkSession.Find also has asynchronous versions called BeginFind and EndFind.


The last thing FindSession does is set the gameState to GameState.FindSession to display the find session menu screen.

Add to the switch statements in the Update and Draw methods so gamers know what to do while you are in this state.

In the game’s Draw method, add the following to the switch statement:

case GameState.FindSession:
FindSessionDraw();
break;

Now, add the FindSessionDraw method to your game using the following code:

// Draw method for the FindSession GameState
private void FindSessionDraw()
{
spriteBatch.Begin();
spriteBatch.DrawString(spriteFont, "FIND SESSION", new Vector2(10, 10), Color.White);
spriteBatch.DrawString(spriteFont, "Exit - Press Back",
new Vector2(10, 50), Color.White);

// Write message if there are no sessions found
if (availableSessions.Count == 0)
{
spriteBatch.DrawString(spriteFont, "NO SESSIONS FOUND",
new Vector2(10, 90), Color.White);
}
else
{
// Print out a list of the available sessions
int sessionIndex = 0;
foreach (AvailableNetworkSession session in availableSessions)
{
spriteBatch.DrawString(spriteFont, session.HostGamertag + " " +
session.OpenPublicGamerSlots +
((sessionIndex == 0) ? " (PRESS A)" : ""),
new Vector2(10, 90 + sessionIndex * 40), Color.White);
sessionIndex++;
}
}

spriteBatch.End();
}


The FindSessionDraw method loops over all of the AvailableNetworkSession instances in the AvailableNetworkSessionCollection and writes out the host Gamertag and how many gamer slots are available.

AvailableNetworkSession provides many properties that expose information about the NetworkSession that you can join. HostGamertag returns the string GamertagCurrentGamerCount, OpenPrivateGamerSlots, and OpenPublicGamerSlots return the total gamers in the session, and the amount of open private and public slots to join. The SessionProperties property returns the NetworkSessionProperties used to create the session. Finally, the QualityOfService property returns an instance of QualityOfService, which tells you how well you can connect to the AvailableNetworkSession. for the host of the session. This might not be the person who started the session because the session might support host migration.

The QualityOfService class provides helpful measurements that enable you to see how well the connection of the machine is to the AvailableNetworkSession. QualityOfService provides a number of properties including AverageRoundtripTime and MinimumRoundtripTime, which returns the average and minimum amount of time it takes for the machine to take to the host of the NetworkSession. The BytesPerSecondDownstream and BytesPerSecondUpstream properties are the estimated download and upload bandwidth from the machine making the call to the host of the NetworkSession.

Next, update the game’s Update method with another condition in the switch statement to handle updating while you are in the FindSession state. Add the following to the switch statement in the Update method:

case GameState.FindSession:
FindSessionUpdate();
break;

Now, add the FindSessionUpdate method to the Game class:

// Update method for the FindSession GameState
private void FindSessionUpdate()
{
// Go back to the main menu
if (ButtonPressed(Buttons.Back))
gameState = GameState.MainMenu;
// If the user presses the A button join the first session
else if (ButtonPressed(Buttons.A) && availableSessions.Count != 0)
JoinSession(0);
}

To keep the sample simple, you don’t handle selecting all of the available network sessions and just pick the first one. In your game, take user input to move some type of selection to enable the user to select which session to join.

Now you come to the point where you need to run the sample on multiple machines. You need one machine to create the session and another to search for available sessions.

Open another instance of Visual Studio and open the same solution file. If you have not already created an Xbox 360 version of the project, you should create a copy of the project for Xbox 360.

In one of the instances of Visual Studio, launch the Windows version of the sample project. Have the player create a session and wait at the lobby. In the other instance of Visual Studio, launch the Xbox 360 version of the sample project. Select the find session menu option. Now you should see something similar to Figure 9 showing the available sessions.

Figure 9. Find session menu screen
Other -----------------
- User Interface : Using the ApplicationBar Control
- User Interface : Creating an Animated Splash Screen
- Windows Phone 7 Game Development : The World of 3D Graphics - Vertex and Index Buffers
- Windows Phone 7 Game Development : The World of 3D Graphics - Hidden Surface Culling
- Windows Phone 7 Game Development : The World of 3D Graphics - The Depth Buffer
- Windows Phone 7 Game Development : The World of 3D Graphics - Rendering 3D Objects
- Windows Phone 7 Game Development : The World of 3D Graphics - Perspective Projection
- Developing for Windows Phone and Xbox Live : Let the 3D Rendering Start
- Developing for Windows Phone and Xbox Live : Reach and HiDef Graphics Profiles
- Developing for Windows Phone and Xbox Live : Graphics Pipeline
- Developing for Windows Phone and Xbox Live : Graphics Pipeline
- Programming Windows Phone 7 : Elements and Properties - More on Images
- Programming Windows Phone 7 : TextBlock Properties and Inlines
- Programming Windows Phone 7 : The Phone’s Photo Library
- Programming Windows Phone 7 : Capturing from the Camera
- Windows Phone 7 : Loading Local Bitmaps from Code
- Windows Phone 7 : Image and ImageSource
- Windows Phone 7 : Images Via the Web
- Windows Phone 7 : Customizing Your E-Mail Signature
- Windows Phone 7 : Managing Mail Folders
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us